/*---------------------------------------------------------------------------
 * 2005 NORSYS
 * main author : nono
 *
 * This software is a computer program whose purpose is to provide abstraction
 * for accessing directory data sources within java applications.
 *
 * This software is governed by the CeCILL  license under French law and
 * abiding by the rules of distribution of free software.  You can  use, 
 * modify and/ or redistribute the software under the terms of the CeCILL
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info". 
 *
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability. 
 *
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or 
 * data to be ensured and,  more generally, to use and operate it in the 
 * same conditions as regards security. 
 *
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
 *
 * Created on Nov 15, 2005
 * --------------------------------------------------------------------------*/

package speculoos.core;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * A class for using mapper over a collection of objects.
 * <p>
 * This class may be used to apply a mapper to a collection of objects. This
 * collection may be passed to the mapper in the following forms:
 * <ul>
 * <li>an array of {@link java.lang.Object} instances, </li>
 * <li>an instance of {@link java.util.Iterator}, </li>
 * <li>an instance of {@link java.util.List}. </li>
 * </ul>
 * The base mapper is set using method {@link #setMapper(Mapper)} and when any
 * one of the colleciton mapping method is called, it is invoked by iterating
 * over the collection according to the specific method used.
 * </p>
 * <p>
 * Each method return an instance of {@link MapperResult} object which
 * encapsulates the number of calls handled correctly - ie. without an exception
 * being thrown - and all thrown exceptions.
 * </p>
 * <p>
 * The basic method inherited from interface mapper assumes the input object is
 * an instance of interface List and simply calls
 * {@link #map(java.util.List, java.util.Map)}.
 * </p>
 * 
 * @author nono
 * @version $Id: CollectionMapper.java 259 2006-05-23 10:34:50Z /C=FR/ST=Nord/L=Lille/O=Norsys SA/OU=UE/CN=Arnaud Bailly/emailAddress=abailly@norsys.fr $
 */
public class CollectionMapper implements Mapper {

	/* base mapper used */
	private Mapper mapper;

	/*
	 * name of this mapper. Default is 'iterator'
	 */
	private String name = "iterator";

	/**
	 * @param name
	 *            The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see speculoos.core.Mapper#map(java.lang.Object, java.util.Map)
	 */
	public Object map(Object input, Map param) throws MapperException {
		return map((List) input, param);
	}

	/***************************************************************************
	 * Applies a map to a list of input objects.
	 * 
	 * @param input
	 *            the input List
	 * @param param
	 *            the environment to use for evaluatoin
	 * @return a MapperResult instance
	 */
	public MapperResult map(List input, Map param) {
		MapperResult mr = new MapperResult();
		for (Iterator it = input.iterator(); it.hasNext();) {
			try {
				Object o = mapper.map(it.next(), param);
				mr.addresult(o);
			} catch (MapperException e) {
				mr.addException(e);
			}
		}
		return mr;
	}

	/***************************************************************************
	 * Applies a map to an iterator over a collection of input objects.
	 * 
	 * @param input
	 *            the input Iterator
	 * @param param
	 *            the environment to use for evaluatoin
	 * @return a MapperResult instance
	 */
	public MapperResult map(Iterator input, Map param) {
		MapperResult mr = new MapperResult();
		while (input.hasNext()) {
			try {
				Object o = mapper.map(input.next(), param);
				mr.addresult(o);
			} catch (MapperException e) {
				mr.addException(e);
			}
		}
		return mr;
	}

	/***************************************************************************
	 * Applies a map to an array of input objects.
	 * 
	 * @param input
	 *            the input array of objects
	 * @param param
	 *            the environment to use for evaluatoin
	 * @return a MapperResult instance
	 */
	public MapperResult map(Object[] input, Map param) {
		MapperResult mr = new MapperResult();
		int ln = input.length;
		for (int i = 0; i < ln; i++) {
			try {
				Object o = mapper.map(input[i], param);
				mr.addresult(o);
			} catch (MapperException e) {
				mr.addException(e);
			}
		}
		return mr;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see speculoos.core.Mapper#getName()
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return Returns the mapper.
	 */
	public Mapper getMapper() {
		return mapper;
	}

	/**
	 * @param mapper
	 *            The mapper to set.
	 */
	public void setMapper(Mapper mapper) {
		this.mapper = mapper;
	}

}
